package com.changgou.seckill.thread;

import com.changgou.entity.IdWorker;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName MultiThreadingCreateOrder
 * @Description 多个用户下单操作
 * @Author 洛尘
 * @Date 11:15 2019/11/28
 * @Version 2.1
 **/
@Component
public class MultiThreadingCreateOrder {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired(required = false)
    private SeckillGoodsMapper seckillGoodsMapper;

    @Async
    public void creadOrder(String time ,Long seckillId,String userId){
        SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();
        try {

            if (seckillStatus != null) {

                SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + time).get(seckillId);
                if (seckillGoods == null || seckillGoods.getStockCount() <= 0) {
                    throw new RuntimeException("该商品已经卖完了");
                }
                // 如果没有被抢购完，可以进行下单操作
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(idWorker.nextId());  // 主键
                seckillOrder.setSeckillId(seckillId);   // 秒杀商品id
                seckillOrder.setMoney(seckillGoods.getCostPrice()); // 支付金额
                seckillOrder.setUserId(userId);         // 当前用户
                seckillOrder.setCreateTime(new Date()); // 下单时间
                seckillOrder.setStatus("0");            // 支付状态：未支付
                redisTemplate.boundHashOps("SeckillOrder" + time).put(userId, seckillOrder);
               /* //  设置秒杀商品的库存
                seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);*/
                Long stockCount = redisTemplate.boundHashOps("SeckillGoodsCount").increment(seckillId.toString(),-1);
                System.out.println(seckillGoods.getStockCount());
                seckillGoods.setStockCount(seckillGoods.getStockCount() + stockCount.intValue());

                if (seckillGoods.getStockCount() <= 0) {
                    // 将数据同步到数据库中，这样就定时任务就不会将该商品些如redis中去
                    seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
                    // 删除redis 中的数据
                    redisTemplate.boundHashOps("SeckillGoods_" + time).delete(seckillId);
                } else {
                    //seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
                    // 更改redis中的货物数量
                    redisTemplate.boundHashOps("SeckillGoods_" + time).put(seckillId, seckillGoods);
                }
            }
        } catch(RuntimeException e){
                e.printStackTrace();
        }
    }

}
